<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
	<meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="format-detection" content="telephone=no,email=no">
    <meta name="screen-orientation" content="portrait">
    <meta name="x5-orientation" content="portrait">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-title" content="商品详情">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <title>商品详情</title>
    <link rel="stylesheet" type="text/css" href="../css/base.css?v=20170821001">
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/layer/3.0.1/mobile/layer.min.js"></script>
    <script src="https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js"></script>
    <script src="../js/common.js?v=20170814001"></script>
    <script src="../js/conf.js?v=20170801001"></script>
    <script>
	$(function(){
		FastClick.attach(document.body);
		
		var params = _CZWeb.getUrlParams();
		var brandid = params["brandid"];
		var loading_index = null;

		var goodsid = params["goodsid"];
		if(!goodsid){
			layer.msg('商品参数错误!')
			return;
		}

		var formatErrorMsg = function(str){
			return str.replace(/{{brand-name}}/g,brandName);
		};

		//弹窗
		var dialog = function(status,str,btn,title,img){
			var dialog = $('<div class="dialog sign-dialog dialog'+status+'"></div>')
			.append('<div class="dialog-close close"></div>')
			.append('<div class="dialog-top">'+(title||'')+'</div>')
			.append(img===false?'':('<div class="dialog-img"'+ (img?('style="background-image:url('+img+')"'):'') +'></div>'))
			.append('<div class="dialog-text">'+(str===false?'':(str?str:(formatErrorMsg(_errorMsg[status])||"发生了系统错误!")))+'</div>');
			var btnBox = $('<div class="button-box flex flex-y-center flex-x-center"></div>');
			if(btn instanceof Array){// [{"text":"asdf","fn":function(){}}]
				$.each(btn, function(index, val) {
					if(typeof val === 'object'){
						var btnid = new Date().getTime() +''+ parseInt(Math.random()*100);
						var b = $('<div class="dialog-btn close '+(val.simple?'simple':'')+'" id="'+btnid+'">'+val.text+'</div>');
						if (typeof this.fn === 'function') {
							$('body').on('click','#'+btnid,function(){
								console.log(val.text);
								val.fn();
							});
						}
						btnBox.append(b.prop("outerHTML"));
					}else{
						btnBox.append('<div class="dialog-btn close">'+(val||'关闭')+'</div>');
					}
				});
			}else{
				if(btn!==false)
					btnBox.append('<div class="dialog-btn close">'+(btn||'关闭')+'</div>');
			}
			dialog.append(btnBox);
			layer.dialog(dialog.prop("outerHTML"));
		};


		var showView = function(data){
			if(data.banner)
				$('.banner').css('backgroundImage','url('+data.banner+'),url(../images/shop/banner_bg.png)');
			$('.head').find('.title').html(data.goodsname).end().find('.price').html(data.price);
			$('.goods-des .info').append(data.goods_des);
			$('.change-des .info').append(data.draw_des).find('.kfphone').append(_kefu[brandid].phone);
			$('.num').append(data.residue_nums);
			if(data.residue_nums<=0){
				$('.buy').addClass('grayscale');
			}
		};

		var getGoodsDetail = function(){
			$.ajax({url: _shopDetailAction,type: 'POST',dataType: 'json',data: params,beforeSend:function(){
				loading_index = layer.loading();
			}})
			.done(function(data) {
				if(data&&data.result==0){
					showView(data);
					_CZWeb.Statistics({pos_tap:"积分乐园-积分商城-显示商品["+data.goodsname+"]"});
				}else{
					layer.msg((data&&data.reason)||'系统错误!');
				}
			})
			.fail(function() {
				layer.msg('请求失败,请检查网络!');
			}).always(function() {
				layer.close(loading_index);
			});
		};

		getGoodsDetail();

		$('.buy').on('click',function(){
			if($(this).hasClass('grayscale')){
				return;
			}

			$.ajax({url: _shopDetailBuyAction,type: 'POST',dataType: 'json',data: params,beforeSend:function(){
				loading_index = layer.loading();
			}})
			.done(function(data) {
				if(data){
					if(data.result==0){
						$('.num').html(data.residue_nums);
						if(data.residue_nums<=0){
							$('.buy').addClass('grayscale');
						}
						dialog("bingo","恭喜您兑换成功",[{"text":"暂不使用","simple":true},{"text":"立即使用","fn":function(){
							location.href = _CZWeb.getAdspaceUrl(brandid,_host+'/bag_list.html','我的奖品',false);
						}}]);
						_CZWeb.Statistics({pos_tap:"积分乐园-积分商城-兑换商品成功"});
					}else if(data.result==20){//积分不足
						dialog(data.result,"你的积分不足哦，赶快去赚积分吧","确定");
						_CZWeb.Statistics({pos_tap:"积分乐园-积分商城-兑换商品积分不足"});
					}else if(data.result==36){//库存不足
						dialog(data.result,"今天已兑完，明天赶早哦","朕知道了");
						_CZWeb.Statistics({pos_tap:"积分乐园-积分商城-兑换商品已兑完"});
					}else{
						dialog(data.result,data.reason);
					}
				}else{
					dialog("error","OH MY GOD,发生了系统错误!");
				}
			})
			.fail(function() {
				dialog('9999');
			}).always(function() {
				layer.close(loading_index);
			});
		});

	});
	</script>
</head>
<body>
	<div class="page page-shop-detail">
		<div class="banner"></div>
		<div class="info">
			<div class="head">
				<div class="title"></div>
				<div class="info"><span class="price"></span>积分</div>
			</div>
			<div class="goods-des">
				<div class="title">商品说明</div>
				<div class="info"></div>
			</div>
			<div class="change-des">
				<div class="title">兑换说明</div>
				<div class="info"></div>
			</div>
		</div>
		<div class="button buy">
			<div class="u">立即兑换</div>
			<div class="d">今日剩余<span class="num"></span>份</div>
		</div>
	</div>
	<script>
	var _hmt = _hmt || [];
	(function() {
	  var hm = document.createElement("script");
	  hm.src = "//hm.baidu.com/hm.js?9b40deeaa9ce094daadf13e0dc1a4729";
	  var s = document.getElementsByTagName("script")[0];
	  s.parentNode.insertBefore(hm, s);
	})();
	</script>
</body>
</html>
